<!-- 我的足迹 -->
 <!--我的足迹-->
<template>
  <div class="product-collection">
    <!-- 商品列表 -->
    <el-row :gutter="20" class="product-list">
      <el-col
        v-for="(product, index) in products"
        :key="index"
        :span="6"
        class="product-item"
      >
        <el-card shadow="hover" class="product-card">
          <img
            :src="product.imageUrl"
            alt="商品图片"
            class="image-placeholder"
            @error="handleImageError(index)"
          />
          <div class="info">
            <div class="number">{{ product.number }}</div>
            <div class="price">¥{{ product.price }}</div>
            <el-button link type="primary" @click="findSimilar(product.id)">
              找相似
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 分页控件 -->
    <el-pagination
      v-model:currentPage="pagination.currentPage"
      :page-sizes="[8, 16, 24]"
      :page-size="pagination.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import axios from 'axios';

const products = ref([]);
const pagination = reactive({
  currentPage: 1,
  pageSize: 8,
  total: 0
});

// 模拟接口地址
const apiUrl = '/api/collections';

const getProducts = async (params) => {
  try {
    const response = await axios.get(apiUrl, { params });
    return response.data;
  } catch (error) {
    console.error('获取商品列表失败:', error);
    return { data: [], total: 0 };
  }
};

const fetchProducts = async () => {
  const { data, total } = await getProducts({
    page: pagination.currentPage,
    limit: pagination.pageSize
  });
  products.value = data;
  pagination.total = total;
};

fetchProducts();

const handleSizeChange = (pageSize) => {
  pagination.pageSize = pageSize;
  fetchProducts();
};

const handleCurrentChange = (currentPage) => {
  pagination.currentPage = currentPage;
  fetchProducts();
};

const handleImageError = (index) => {
  // 替换为默认图片
  products.value[index].imageUrl = '/default-image.png';
};

const findSimilar = (id) => {
  // 处理查找相似商品逻辑，例如导航到详情页
  console.log('查看相似商品:', id);
};
</script>

<style scoped>
.product-collection {
  padding: 20px;
}

.product-list {
  margin-bottom: 20px;
}

.product-card {
  height: 250px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 15px;
}

.image-placeholder {
  width: 100%;
  height: 180px;
  object-fit: cover;
  margin-bottom: 10px;
  background-color: #f5f5f5;
}

.info {
  text-align: center;
}

.number {
  font-size: 14px;
  color: #666;
}

.price {
  font-weight: bold;
  color: #f56c6c;
  margin-top: 5px;
}

.el-button {
  margin-top: 10px;
  color: #999;
  border-color: #dcdfe6;
}

.el-button:hover {
  color: #666;
  border-color: #c0c4cc;
}
</style>